<?php


namespace Convert\Controller;

use Think\Controller;

class IndexController extends BaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }


    public function index()
    {


        $this->display();
    }


    public function convert()
    {
        $table_list = table_list();
        $this->assign('table_list', $table_list);

        $path = './Application/Convert/Data/index.json';
        $json = file_get_contents($path);
        $data= json_decode($json,true);

        $this->assign('data', $data);
        $this->display();
    }




    protected  function getNext($current)
    {
        $table_list = table_list();
        $next = get_next($table_list, $current);
        return $next?$next['name']:'finish';
    }





    protected function convert_field($data){
        foreach($data as &$v){
            $v['role_id'] =1;
        }
        unset($v);
        return $data;
    }


    protected function convert_avatar($data){
        foreach($data as &$v){
            $v['path'] ='/'.$v['path'];
        }
        unset($v);
        return $data;
    }


    protected function convert_member($data){
        foreach($data as &$v){
            $v['last_login_role'] =1;
            $v['show_role'] =1;
            $v['score1'] =$v['score'];
            $v['score4'] =$v['tox_money'];
            $role_data[] = array('uid'=>$v['uid'],'role_id'=>1,'status'=>1,'step'=>'finish','init'=>1);
        }
        unset($v);
        truncate_table('user_role');
        $role_data = array_chunk($role_data, 100);
        foreach ($role_data as $v) {
            M('user_role')->addAll($v);
        }

        return $data;
    }

    protected function convert_ucenter_member($data){
        foreach($data as &$v){
            $v['type'] =1;
        }
        unset($v);
        return $data;
    }



    protected function convert_config($data){
        return $data;

    }


    protected function finish(){


        $content = file_get_contents('./Conf/user.php');
        $content = preg_replace('/\'UC_AUTH_KEY\', \'.*?\'/i', '\'UC_AUTH_KEY\', \'' . $this->db_config['data_auth_key'] . '\'', $content);
        file_put_contents('./Conf/user.php', $content);


        $content_common = file_get_contents('./Conf/common.php');
        preg_match('/DATA_AUTH_KEY\'.*?\'(.*?)\'/i' , $content_common,$match);

        if($match[1] != $this->db_config['data_auth_key']){
            $content_common = preg_replace('/'.$match[1].'/',$this->db_config['data_auth_key'] , $content_common);
            file_put_contents('./Conf/common.php', $content_common);
        }

        $return['status'] = 1;
        $return['info'] = '转移成功。';
        $this->ajaxReturn($return);
    }



}